<?php
/**
 * User: Allen
 * Date: 15-10-14
 * 保险险种控制器
 */

class Insurance extends PC_Controller{
	
	public function __construct(){
		parent::__construct();
		
		$this->load->model('insurance_model');
		$this->load->model('version_model');
	}
	
	/**
	 * @brief 保险险种信息页面
	 * @param Null
	 * @par 2015/10/14 Ver 1.00 Created by Allen
	 */	
	public function insurance_list() {
		$this->load->view('/www/insurance_list.php',$this->data);
	}
	
	/**
	 * @brief ajxa获取保险公司信息
	 * @param Null
	 * @par 2015/10/14 Ver 1.00 Created by Allen
	 */
	public function ajax_insurance_list() {
		$data = $_GET;
		$start = $data['iDisplayStart']; //显示的起始索引
		$length = $data['iDisplayLength'];//显示的行数
		$sort_th = $data['mDataProp_'.$data['iSortCol_0'].''];//被排序的列 
		$sort_type = $data['sSortDir_0'];//排序的方向 "desc" 或者 "asc".
		$search = $data ['sSearch'];//全局搜索字段
		$where['status'] = 1;
		$where['concat(id) like'] = '%'.trim($search).'%';
		$this->data['count'] = $this->insurance_model->getCount($where);
		$aaData = array();
		if($this->data['count']) {
			$order_by = $sort_th." ".$sort_type;
			$insurance_list = $this->insurance_model->getInsuranceList($where,$length,$start,$order_by);
			$this->load->helper('string_helper');
			foreach($insurance_list as $key => $value) {
				if($value['remark']) {
					$value['intro'] = cn_substr_utf8($value['remark'],70);
					if($value['intro'] == $value['remark']) {
						$value['intro'] = $value['remark'];
					}else {
						$value['intro'] = $value['intro']."...";
					}
				}else {
					$value['intro'] = '';
				}
				if($value['button_status'] == 1) {
					$insurance_list[$key]['button_status'] = '开';
				}else {
					$insurance_list[$key]['button_status'] = '关';
				}
				$id_array = explode(',',substr($insurance_list[$key]['version_id'],0,strlen($insurance_list[$key]['version_id'])-1));
				$combon = '';
				foreach($id_array as $k => $val) {
					$combon_where['id'] = $val; 
					$version_info = $this->version_model->checkVersion($combon_where);
					if($k + 1 < count($id_array)) {
						$combon .=$version_info['version_name'].",";						
					}else {
						$combon .=$version_info['version_name'];
					}
				}
				$insurance_list[$key]['combo'] = $combon;
				$insurance_list[$key]['remark'] = '<div class="intro" style="position:relative;cursor:pointer;">'.$value['intro'].'<div class="ser_intro">'.$value['remark'].'<i></i></div></div>';
				$del_oper = "onclick=\"if(confirm('确实要删除此条记录吗？')) return true;else return false; \"";
				$edit_url = '<a href="/www/insurance/edit_page/'.$value['id'].'"><span class="btn btn-xs btn-primary">编辑</span></a>';
				$del_url = '<a href="/www/insurance/del/'.$value['id'].'"  '.$del_oper.'><span class="btn btn-xs btn-danger cancel">删除</span></a>';
				$insurance_list[$key]['operate'] = $edit_url." ".$del_url;
			}
			$aaData = $insurance_list;
	
		}
		$output['aaData'] = $aaData;
		$output['sEcho'] = $_GET['sEcho'];
		$output['iTotalDisplayRecords'] =  $this->data['count']; //总共有几条数据
		$output['iTotalRecords'] = $this->data['count']; //总共有几条数据
		echo json_encode($output); //最后把数据以json格式返回
	}
	
	/**
	 * @brief 进入编辑页面
	 * @param Null
	 * @par 2015/10/14 Ver 1.00 Created by Allen
	 */
	public function edit_page() {
		$id = $this->uri->segment(4);
		$where['id'] = $id;
		$insurance_info = $this->insurance_model->checkInsurance($where);
		$id_array = explode(',',substr($insurance_info['version_id'],0,strlen($insurance_info['version_id'])-1));
		$sum_list = unserialize($insurance_info['sum_list']);
		$version_where['status'] = 1;
		$version_list = $this->version_model->getversionAll($version_where);
		$this->data['version_list'] = $version_list;
		$this->data['data'] = $insurance_info;
		$this->data['id_array'] = $id_array;
		$this->data['sum_list'] = $sum_list;
		$this->load->view('/www/insurance_edit',$this->data);
	}
			
	/**
	 * @brief 保存编辑信息
	 * @param Null
	 * @par 2015/10/14 Ver 1.00 Created by Allen
	 */
	public function edit() {
		$data = $_POST;
		$temp_sum =array_filter($data['sum_list']);
		if(empty($temp_sum)) {
			$data['way'] = 1;
		}else {
			$data['way'] = 2;
		}
		$id_str = '';
		foreach($data['version_id'] as $key => $value) {
			$id_str .=$value.",";
		}
		$data['version_id'] = $id_str;
		$data['sum_list'] = serialize(array_filter($data['sum_list']));
		$data['update_time'] = time();
		$where['id'] = $data['id'];
		$edit_result = $this->insurance_model->editInsurance($where,$data);
		$go_url = '/www/insurance/insurance_list';
		if($edit_result) {
			$this->location_href($this->lang->line('edit_success'),$go_url);	
		}else {
			$this->location_href($this->lang->line('edit_false'),$go_url);	
		}
	}
	
	/**
	 * @brief 检查保险险种名唯一性
	 * @param Null
	 * @par 2015/10/14 Ver 1.00 Created by Allen
	 */
	public function check_sole() {
		$data = $_POST;
		if(isset($data['id'])) {
			$sole_where['id !='] = $data['id'];
		}
		$sole_where['name'] = $data['name'];
		$sole_where['status'] = 1;
		$sole = $this->insurance_model->checkInsurance($sole_where);
		if($sole) {
			$message = false;
		}else {
			$message = true;
		}
		echo json_encode($message);
	}
	
	/**
	 * @brief 删除保险公司信息
	 * @param Null
	 * @par 2015/10/14 Ver 1.00 Created by Allen
	 */
	public function del() {
		$id = $this->uri->segment(4);
		$data['update_time'] = time();
		$data['status'] = 0;
		$where['id'] = $id;
		$del_result = $this->insurance_model->editInsurance($where,$data);
		$go_url = '/www/insurance/insurance_list';
		if($del_result) {
			$this->location_href($this->lang->line('del_success'),$go_url);
		}else {
			$this->location_href($this->lang->line('del_false'),$go_url);
		}
	}

	/**
	 * @brief 进入添加页面
	 * @param Null
	 * @par 2015/10/14 Ver 1.00 Created by Allen
	 */
	public function add_page() {
		$where['status'] = 1;
		$version_list = $this->version_model->getversionAll($where);
		$this->data['version_list'] = $version_list;
		$this->load->view('/www/insurance_add',$this->data);
	}
	
	/**
	 * @brief 保存添加信息
	 * @param Null
	 * @par 2015/10/14 Ver 1.00 Created by Allen
	 */
	public function add() {
		$data = $_POST;
		$temp_sum = array_filter($data['sum_list']);
		if(empty($temp_sum)) {
			$data['way'] = 1;
		}else {
			$data['way'] = 2;
		}
		$id_str = '';
		foreach($data['version_id'] as $key => $value) {
			$id_str .=$value.",";
		}
		$data['version_id'] = $id_str;
		$data['sum_list'] = serialize(array_filter($data['sum_list']));
		$data['create_time'] = $data['update_time'] = time();
		$add_result = $this->insurance_model->addInsurance($data);
		$go_url = '/www/insurance/insurance_list';
		if($add_result) {
			$this->location_href($this->lang->line('add_success'),$go_url);
		}else {
			$this->location_href($this->lang->line('add_false'),$go_url);
		}
	}
}
?>